package com.xxx.yuedong.mine.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.mine.controller.form.*;
import com.xxx.yuedong.mine.pojo.User;
import com.xxx.yuedong.mine.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2023-07-31
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public R login(@Valid @RequestBody UserLoginForm form){
        log.debug("正在 【执行登录请求】,参数为:{}",form.toString());
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        HashMap map = userService.login(param);
        Long userId = MapUtil.getLong(map, "id");
        log.debug("userId={}",userId);
        R r = R.ok(map).put("result",userId != null ? true : false);
        if (userId!=null){
            StpUtil.login(userId);
            ArrayList<String> permissions = userService.searchUserPermission(userId);
            String token=StpUtil.getTokenInfo().getTokenValue();
            r.put("permissions",permissions).put("token",token);
        }
        return r;
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册")
    public R register(@Valid @RequestBody InsertUserForm form){
        if (userService.searchUserByUsername(form.getUsername())){
            return R.error("用户已经存在");
        }
        User user = JSONUtil.parse(form).toBean(User.class);
        user.setCreateTime(LocalDateTime.now());
        user.setStatus(0);
        user.setAvatar("https://yuedong-1305949163.cos.ap-chengdu.myqcloud.com/avater/38de1b97232f4bbcb237b4c0eb84a613.jpg");
        user.setModifyTime(LocalDateTime.now());
        user.setProfile("这个人很懒什么都没有留下！");
        user.setRole(3);
        int rows = userService.insert(user);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/insertGuidePageData")
    @ApiOperation(value = "插入引导页数据")
    public R insertGuidePageData(@Valid @RequestBody InsertGuidePageDataForm form){
        HashMap param = new HashMap();
        param.put("height", form.getHeight());
        param.put("weight", form.getWeight());
        param.put("sex", form.getSex());
        param.put("tags", JSONUtil.parse(form.getTags()).toBean(String.class));
        param.put("userId", StpUtil.getLoginIdAsLong());
        param.put("status", 1);

        int rows = userService.insertGuidePageData(param);

        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchUserPermission")
    @ApiOperation(value = "查询权限")
    public R searchUserPermission(){
        ArrayList<String> list = userService.searchUserPermission(1l);
        return R.ok().put("list",list);
    }

    @PostMapping("/searchUserInfo")
    @ApiOperation(value = "查询用户信息")
    @SaCheckLogin
    public R searchUserInfo(@Valid @RequestBody SearchUserInfoForm form){
        HashMap map = userService.searchUserInfo(form.getUserId());
        return R.ok(map);
    }

    @PostMapping("/searchAllUserByPage")
    @ApiOperation(value = "查询所有用户信息")
    @SaCheckLogin
    public R searchAllUserByPage(@Valid @RequestBody SearchAllUserByPageForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        return userService.searchAllUserByPage(param);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改信息（管理员）")
    @SaCheckLogin
    public R update(@Valid @RequestBody UpdateUserForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        return userService.update(param);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    public void logout(){
        StpUtil.logout();
    }
}

